package com.sundi.ecgoods.web;


import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sundi.ecgoods.entity.EcComment;
import com.sundi.ecgoods.entity.EcGoods;
import com.sundi.ecgoods.service.IEcCommentService;
import com.sundi.ecgoods.vo.CommontVo;
import com.sundi.ecgoods.vo.PageVo;
import com.sundi.ecgoods.vo.ResultEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * <p>
 * 评论表 前端控制器
 * </p>
 *
 * @author jgn
 * @since 2020-02-28
 */
@RestController
@RequestMapping("/comment")
public class EcCommentController {
    @Autowired
    IEcCommentService commentService;

    /**
     * 列表+分页+查询
     * @param
     * @return
     */
    @RequestMapping("list")
    public ResultEntity list(CommontVo commontVo){
        PageHelper.startPage(commontVo.getPageNo(),commontVo.getPageSize());
        Wrapper<EcComment> wrapper = new EntityWrapper<>();
        wrapper.eq("deleted",0);
        if(commontVo.getUserId()!=null){
            wrapper.eq("user_id",commontVo.getUserId());
        }
        if(commontVo.getValueId()!=null){
            wrapper.eq("value_id",commontVo.getValueId());
        }
        List<EcComment> comments = commentService.selectList(wrapper);
        PageInfo<EcComment> pageInfo=new PageInfo<>(comments);
        return ResultEntity.ok(pageInfo);
    }

    /**
     * 逻辑删除
     * @param id
     * @return
     */
    @RequestMapping("delete")
    public ResultEntity delete(Integer id){
        Wrapper<EcComment> wrapper = new EntityWrapper<>();
        wrapper.eq("id",id);
        EcComment comment = commentService.selectOne(wrapper);
        comment.setDeleted(1);
        commentService.updateById(comment);
        return ResultEntity.ok("逻辑删除成功");
    }

    /**
     * 回复评论
     * @param commontVo
     * @return
     */
    @RequestMapping(value = "add")
    public ResultEntity add(@RequestBody CommontVo commontVo){
        Wrapper<EcComment> wrapper = new EntityWrapper<>();
        wrapper.eq("id",commontVo.getId());
        EcComment comment = commentService.selectOne(wrapper);
        comment.setAdminContent(commontVo.getAdminContent());
        commentService.updateById(comment);
        return ResultEntity.ok("回复评论成功");
    }

}
